package com.bdqn.dao;



import com.bdqn.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**公共的增删改查的方法
 * @author liu jiaxin  2019/9/18
 */
public abstract class BaseDao {

    protected Connection conn;
    protected PreparedStatement ps;
    protected ResultSet rs;

    //增删改
    public int upDate(String sql,Object... params ){
        int updateRows=0;
        //获取连接
        try(Connection conn= JDBCUtils.getConnection();
            PreparedStatement stm=conn.prepareStatement(sql)) {
            for (int i = 0; i <params.length ; i++) {
                stm.setObject(i+1,params[i]);
            }
            updateRows = stm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateRows;
    }


    /**
     * 增删改
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(Connection connection,String sql,Object... params ){
        int updateRows=0;
        //获取连接
        try(PreparedStatement stm=connection.prepareStatement(sql)) {
            for (int i = 0; i <params.length ; i++) {
                stm.setObject(i+1,params[i]);
            }
            updateRows = stm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateRows;
    }


    //公共的查询方法
    public ResultSet executeQuery(String sql,Object... params){
        try{
            conn= JDBCUtils.getConnection();
            ps=conn.prepareStatement(sql);
            for (int i = 0; i <params.length ; i++) {
                ps.setObject(i+1,params[i]);
            }
            rs=ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
            closeAll();
        }
        return rs;
    }

    /**
     * 获取唯一结果的方法
     * @param sql
     * @param params
     * @return
     */
    public Object uniqueResult(String sql,Object...params){
        try( //获取连接
             Connection conn = JDBCUtils.getConnection();
             //执行SQL
             PreparedStatement stm=conn.prepareStatement(sql)) {
            for (int i = 0; i < params.length; i++) {
                stm.setObject(i+1,params[i]);
            }
            try(ResultSet rs = stm.executeQuery()) {
                while (rs.next()) {
                    return rs.getObject(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 公共的批量执行增删改的方法
     *
     * @param sql        带"?"占位符的SQL
     * @param paramsList 批量插入参数的数组集合
     * @return
     */
    public void executeBatch(String sql, List<Object[]> paramsList) {
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            //关闭自动提交
            conn.setAutoCommit(false);
            for (Object[] params : paramsList) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
                ps.addBatch();
            }
            ps.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                conn.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            closeAll();
        }
    }

    protected void closeAll(){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
